package com.hellobike.haxing.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.common.constant.CommonConstant;
import com.hellobike.haxing.common.util.DateUtils;
import com.hellobike.haxing.common.util.RedisUtil;
import com.hellobike.haxing.modules.order.constant.OrderConstants;
import com.hellobike.haxing.modules.order.entity.OrderB;
import com.hellobike.haxing.modules.order.mapper.OrderBMapper;
import com.hellobike.haxing.modules.order.mapper.OrderMapper;
import com.hellobike.haxing.modules.order.send.OrderCancelBackendSend;
import com.hellobike.haxing.modules.order.service.IOrderBService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: B端订单
 * @Author: haxing-boot
 * @Date: 2020-02-08
 * @Version: V1.0
 */
@Service
public class OrderBServiceImpl extends ServiceImpl<OrderBMapper, OrderB> implements IOrderBService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderBMapper orderBMapper;
    @Autowired
    private OrderCancelBackendSend orderCancelBackendSend;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void updateFinishOrder() {
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(8);
        Page<OrderB> page = new Page<>(1, 10000);
        QueryWrapper<OrderB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderB::getDeleted, CommonConstant.DEL_FLAG_0)
                .in(OrderB::getBstatus, OrderConstants.ORDER_STATUS_3, OrderConstants.ORDER_STATUS_5)
                .le(OrderB::getCollectTime, localDateTime).gt(OrderB::getCollectTime, localDateTime.minusDays(20));
        IPage<OrderB> pageResult = super.page(page, queryWrapper);
        List<Long> orderIds = new ArrayList<>();
        List<Long> ids = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(pageResult.getRecords())) {
            for (OrderB orderB : pageResult.getRecords()) {
                orderIds.add(orderB.getOrderId());
                ids.add(orderB.getId());
            }
        }
        if (CollectionUtils.isNotEmpty(orderIds)) {
            orderMapper.updateFinishOrder(orderIds);
            baseMapper.updateFinishOrder(ids);
        }

    }

    @Override
    public void updateCancel(Long orderId) {
        OrderB orderb = orderBMapper.getOrderB(orderId);
        if (orderb == null) {
            return;
        }
        int t = 0;
        if (!OrderConstants.ORDER_STATUS_0.equals(orderb.getBstatus())) {
            t = 1;
        }
//        log.info("取消订单==orderId=={}=", orderId);
        orderb.setBstatus(OrderConstants.ORDER_STATUS_2);
        orderb.setUpdateTime(DateUtils.getDate());
        boolean flag = super.updateById(orderb);
        if (flag && t == 1) {
            orderCancelBackendSend.sendOrderCancel(orderId);
        }
    }
}
